package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.SystemMenu;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.query.SystemMenuQueryObject;
import cn.wolfcode.wms.service.ISystemMenuService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import cn.wolfcode.wms.vo.SystemMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("systemMenu")
public class SystemMenuController {
    @Autowired
    private ISystemMenuService service;

    @RequiredPermission("菜单列表")
    @RequestMapping("list")
    public String list(Map map, @ModelAttribute("qo") SystemMenuQueryObject qo) {
        map.put("parents",service.listAllparent(qo.getParentId()));
        PageResult pageResult = service.query(qo);
        map.put("pageResult", pageResult);
        return "systemMenu/list";
    }

    @RequiredPermission("菜单编辑")
    @RequestMapping("input")
    public String input(Map map, Long id) {
        if (id != null) {
            SystemMenu systemMenu = service.get(id);
            map.put("systemMenu", systemMenu);
        }
        return "systemMenu/input";
    }

    @RequiredPermission("菜单保存或更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(SystemMenu systemMenu) {
        try {
            service.saveOrUpdate(systemMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return JSONResult.success("操作成功");
    }

    @RequiredPermission("菜单删除")
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        if (id != null) {
            try {
                service.delete(id);
            } catch (Exception e) {
                return JSONResult.mark(e.getMessage());
            }
        }
        return JSONResult.success("删除成功");
    }

    @RequestMapping("loadTree")
    @ResponseBody
    public List<SystemMenuVo> loadTree(String parentSn) {
        return service.loadTree(parentSn);
    }
}
